// packageB/pages/cash/cash.js
var common = require("../../../public")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr: [
      {
        price: 1,

      },
      {
        price: 10,

      },
      {
        price: 30,

      }

    ],
    act: -1,
    usefullMoney: '',
    names: '',
    amount: '',
    showPayPwdInput: false,
    pwdVal: '',  //输入的密码
    payFocus: true, //文本框焦点
    flags: false,
    showpop: false,//提现的弹出层
    isLoading: false, //提现申请的加载
    backToApp: false
  },
  showPopup() {
    this.setData({ showpop: true });
  },

  onClose() {
    this.setData({ showpop: false, backToApp: false });
  },
  // 立即提现
  showInputLayer: function () {
    this.setData({ showPayPwdInput: true, payFocus: true });
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayer: function () {

    var val = this.data.pwdVal;

    this.setData({ showPayPwdInput: false, payFocus: false, }, function () {
      // 焦点失去隐藏密码框的时候调取接口
      this.cash_fn()
    });

  },
  // 提现接口
  cash_fn() {
    this.setData({
      isLoading: true
    })
    var that = this
    common.post('/withdrawals/wxxcx', {
      userId: this.data.userInfo.id,
      token: this.data.token,
      amount: that.data.amount,
      // unionId: app.globalData.unionId,uid
      unionId: that.data.uid,
      payPassword: this.data.pwdVal
    }, res => {
      this.setData({
        isLoading: false
      })
      if (res.status == 200) {
        //  common.showToast(res.msg)
        this.setData({
          backToApp: true
        }, () => {
          wx.navigateTo({
            url: '/pages/mySelf/wallet/index',
          })
        })
      } else {
        common.showToast(res.msg)
      }
    })
  },
  /**
   * 获取焦点
   */
  getFocus: function () {
    this.setData({ payFocus: true });
  },
  /**
   * 输入密码监听
   */
  inputPwd: function (e) {
    this.setData({ pwdVal: e.detail.value });

    if (e.detail.value.length >= 6) {
      this.hidePayLayer();
    }
  },
  // 立即提现的按钮
  click_ti() {
    var that = this
    if (this.data.amount == '') {
      common.showToast('请选择提现金额')
      return
    }
    if (this.data.usefullMoney < this.data.amount) {
      this.showPopup()
      return
    }
    if (this.data.amount >= 30) {
      this.showInputLayer()
    } else {
      // 小于30的直接提现
      this.cash_fn()
    }

  },
  //跳转绑定页面
  click_bind() {
    wx.navigateTo({
      url: '/packageB/pages/cashBind/cashBind',
    })
  },
  // 忘记密码
  click_forget() {
    wx.navigateTo({
      url: '/packageB/pages/Paypassword/Paypassword',
    })
  },
  // 点击
  click_act(e) {
    var that = this
    console.log(e.currentTarget.dataset.item.price)
    let indexs = e.currentTarget.dataset.index
    this.setData({
      act: indexs,
      amount: e.currentTarget.dataset.item.price
    }, () => {
      if (that.data.amount == 1) {
        that.setData({
          flags: true
        })
      } else if (that.data.amount >= 30) {
        that.setData({
          flags: false
        })
      } else {
        that.setData({
          flags: false
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      usefullMoney: options.usefullMoney ? options.usefullMoney : ""
    })
    var that = this
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        console.log(app.globalData.unionId, "缓存获得的")
        that.setData({
          userInfo: data.user,
          token: data.token,
          openid: data.openid,

        }, function () {
          this.getData()  //获取这张会员卡的信息

        })
      } else {

        that.setData({
          userInfo: null,
          token: '',
        })
      }
    }, true)

  },
  getData() {
    var that = this
    common.get('/user/getUserWxBindingInfo', {
      userId: this.data.userInfo.id,
      token: this.data.token,
    }, res => {
      console.log(res.data[0], "接口获得的")
      that.setData({
        names: res.data[0].userName,
        uid: res.data[0].unionid
      })

    })
  },

})